package com.tushikeji.facecar.db;

import android.os.Environment;

import com.google.gson.Gson;
import com.guo.android_extend.java.ExtInputStream;
import com.tushikeji.facecar.bean.UserBean;
import com.tushikeji.facecar.utils.Base642StrUtils;
import com.arcsoft.facerecognition.AFR_FSDKFace;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by gqj3375 on 2017/7/11.
 */

public class FaceDB {

    private final String TAG = this.getClass().getSimpleName();

    public static String appid = "9RvgfUjch9a54chQuwSqW9pLge9psHijuEVjYed2LsbP";
    public static String ft_key = "5XKrQ1ih8hHaKUe79Xsk4WVNjTGTvRMhGNvrH8XJTb5T";
    public static String fd_key = "5XKrQ1ih8hHaKUe79Xsk4WVVtrXdKuGAecwY4vCEduCb";
    public static String fr_key = "5XKrQ1ih8hHaKUe79Xsk4WVd4FnnrvR25CPZg2Y654ap";
    public static String age_key = "5XKrQ1ih8hHaKUe79Xsk4WWEsG6e6C2MJfm4ijsE9CwP";
    public static String gender_key = "5XKrQ1ih8hHaKUe79Xsk4WWN2fMpV1SNmyChrPYGqsRV";


    //基础目录
    public static String mBaseStr = Environment.getExternalStorageDirectory() + "/Face/";
    //图片目录
    public static String mStr = Environment.getExternalStorageDirectory() + "/Face/IMG";
    //特征目录
    public static String mFeatureStr = Environment.getExternalStorageDirectory() + "/Face/Feature";
    //識別成功目录
    public static String mPictureStr = Environment.getExternalStorageDirectory() + "/FacePicture";
    public static String mImageStr = Environment.getExternalStorageDirectory() + "/Face/Image";
    private UserBean userBean;
    public List<FaceRegist> mRegister;

    public class FaceRegist {
        @Override
        public String toString() {
            return "FaceRegist{" +
                    "mName='" + mName + '\'' +
                    ", mSex='" + mSex + '\'' +
                    ", mAge='" + mAge + '\'' +
                    ", mAccountid='" + mAccountid + '\'' +
                    ", level='" + mLevel + '\'' +
                    ", mFaceList=" + mFaceList +
                    '}';
        }

        public String mName;
        public String mSex;
        public String mAge;
        public String mAccountid;
        public String mLevel;
        public List<AFR_FSDKFace> mFaceList;


        public FaceRegist(String name, String sex, String age, String accountid, String level) {
            mName = name;
            mSex = sex;
            mAge = age;
            mAccountid = accountid;
            mLevel = level;
            mFaceList = new ArrayList<>();

        }
    }

    public FaceDB() {
        CheckDir(mBaseStr);
        CheckDir(mStr);
        CheckDir(mFeatureStr);
        CheckDir(mPictureStr);
        CheckDir(mImageStr);
        mRegister = new ArrayList<>();
    }

    private void CheckDir(String path) {
        File file = new File(path);
        if (!file.exists()) {
            file.mkdir();
        }
    }

    public boolean loadInfo() {
        try {
            FileInputStream fs = new FileInputStream(mBaseStr + "name.txt");
            ExtInputStream bos = new ExtInputStream(fs);
            String json = bos.readString();
            String s = Base642StrUtils.DecodeBase642Json(json);
            userBean = new Gson().fromJson(s, UserBean.class);
            bos.close();
            fs.close();
            for (int i = 0; i < userBean.userList.size(); i++) {
                if (new File(mFeatureStr + "/" + userBean.userList.get(i).accountid + ".data").exists()) {
                    mRegister.add(new FaceRegist(userBean.userList.get(i).name, userBean.userList.get(i).sex, userBean.userList.get(i).age, userBean.userList.get(i).accountid, userBean.userList.get(i).level));
                }
            }
            return true;
        } catch (FileNotFoundException e) {
            ArrayList<UserBean.User> userList = new ArrayList<>();
            userList.clear();
            userBean = new UserBean();
            userBean.userList = userList;
            e.printStackTrace();
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public boolean loadFaces() {
        if (loadInfo()) {
            try {
                for (FaceRegist face : mRegister) {
                //    Log.d(TAG, "load name:" + face.mName + "'s face feature data.");
                    FileInputStream fs = new FileInputStream(mFeatureStr + "/" + face.mAccountid + ".data");
                    ExtInputStream bos = new ExtInputStream(fs);
                    AFR_FSDKFace afr = null;
                    afr = new AFR_FSDKFace();
                    if (afr != null) {
                        afr.setFeatureData(bos.readBytes());
                        face.mFaceList.add(afr);
                    }
                    bos.close();
                    fs.close();
               //     Log.d(TAG, "load name: size = " + face.mFaceList.size());
                }
                return true;
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return false;
    }
}
